package com.karaoke.android.home.widget

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import com.karaoke.android.home.R
import com.labo.kaji.relativepopupwindow.RelativePopupWindow

/**
 * Created by Allen on 16/8/19.
 */
class PopupButton @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : FrameLayout(
        context,
        attrs,
        defStyleAttr)
{
    var title: String = ""
        set(value)
        {
            field = value
            titleView.text = value
        }
    var icon: Int = 0
        set(value)
        {
            field = value
            iconBtn.setImageResource(value)
        }
    var iconBackground: Int = 0
        set(value)
        {
            field = value
            iconBtn.setBackgroundResource(value)
        }
    var mPopupWindowBackgroundResId: Int = 0

    private val titleView: TextView
    val iconBtn: ImageButton

    private val popupWindow by lazy { ButtonPopup(context) }
    private var onClickListener: OnClickListener? = null

    override fun setOnClickListener(l: OnClickListener?)
    {
        onClickListener = l
    }

    init
    {
        View.inflate(context, R.layout.popupbutton, this)
        titleView = findViewById(R.id.title) as TextView
        iconBtn = findViewById(R.id.icon) as ImageButton

        val a = context.obtainStyledAttributes(attrs, R.styleable.PopupButton, defStyleAttr, 0)
        try
        {
            if (!isInEditMode)
            {
                title = a.getNonResourceString(R.styleable.PopupButton_titleString) ?: a.getNonResourceString(R.styleable.PopupButton_titleString)
                icon = a.getResourceId(R.styleable.PopupButton_iconBitmap, 0)
                iconBackground = a.getResourceId(R.styleable.PopupButton_iconBackground, 0)
                mPopupWindowBackgroundResId = a.getResourceId(R.styleable.PopupButton_popupWindowBackground, 0)
                iconBtn.setBackgroundResource(if (iconBackground > 0) iconBackground else R.mipmap.ic_popbutton_background)
                isClickable = true
                isLongClickable = true
            }
        }
        finally
        {
            a.recycle()
        }

    }

    override fun onTouchEvent(me: MotionEvent): Boolean
    {
        if (me.action == MotionEvent.ACTION_DOWN)
        {
            popupWindow.showOnAnchor(iconBtn, RelativePopupWindow.HorizontalPosition.ALIGN_RIGHT, RelativePopupWindow.HorizontalPosition.CENTER)

        }
        if (me.action == MotionEvent.ACTION_UP)
        {
            if (popupWindow.isShowing)
            {
                popupWindow.dismiss()
            }
            onClickListener?.onClick(this)
        }
        return super.onTouchEvent(me)
    }

    inner class ButtonPopup(context: Context) : RelativePopupWindow(context)
    {
        init
        {
            val view = View.inflate(context, R.layout.popup_button, null)
            view.setBackgroundResource(if (mPopupWindowBackgroundResId > 0) mPopupWindowBackgroundResId else R.mipmap.popbutton_popup_background)
            (view.findViewById(R.id.icon) as ImageView).setImageResource(icon)
            contentView = view
            width = ViewGroup.LayoutParams.WRAP_CONTENT
            height = ViewGroup.LayoutParams.WRAP_CONTENT
            isFocusable = true
            isOutsideTouchable = true
            setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            {
                animationStyle = 0
            }
        }

        override fun showOnAnchor(anchor: View, vertPos: Int, horizPos: Int)
        {
            (contentView.findViewById(R.id.icon) as ImageView).setImageResource(icon)
            super.showOnAnchor(anchor, vertPos, horizPos)
        }
    }
}